package com.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineCountListener implements HttpSessionListener {
	ServletContext ctx=null;
	@Override
	public void sessionCreated(HttpSessionEvent se) {
		ctx = se.getSession().getServletContext();
		Integer onlinecount = (Integer)ctx.getAttribute("onLineCount");
		if(onlinecount==null) {
			onlinecount = new Integer(1);
		}else {
			int count = onlinecount.intValue();
			onlinecount = new Integer(count+1);
		}
		ctx.setAttribute("onLineCount", onlinecount);
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		ctx = se.getSession().getServletContext();
		Integer onlinecount = (Integer)ctx.getAttribute("onLineCount");
		if(onlinecount==null) {
			onlinecount = new Integer(0);
		}else {
			int count = onlinecount.intValue();
			onlinecount = new Integer(count-1);
		}
		ctx.setAttribute("onLineCount", onlinecount);
	}
	
}
